package gui.menubar;

import static com.nightpiter.controller.log.Logger.log;
import static com.nightpiter.controller.log.Logger.logException;
import gui.view.android.AndroidDrawersRegistry;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import com.nightpiter.controller.log.Logger;
import com.nightpiter.view.drawer.MetroDrawer;

import model.MetroModel;
import model.readers.ReadersRegistry;
import model.savers.MetroSaversRegistry;

public class AddDrawerMenuItem extends JMenuItem{
	public AddDrawerMenuItem() {
		super("Add android drawer");
		addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

				openJarDialog();


			}

			

			
		});



	}
	
	private boolean isDrawer(String name, String path) {
		try {
			URLClassLoader loader = new URLClassLoader(new URL[]{new URL("file:///"+path)});
			try {
				return com.nightpiter.view.drawer.MetroDrawer.class.isInstance(loader.loadClass("com.nightpiter.view.drawer.simple.SimpleMetroDrawer").newInstance());
			} catch (ClassNotFoundException e) {
				return false;
			} catch (InstantiationException e) {
				return false;
			} catch (IllegalAccessException e) {
				return false;
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}

		return false;
	}
	
	private void openJarDialog() {
		JFileChooser fc = new JFileChooser();
		
		fc.setFileFilter(new FileNameExtensionFilter("Java Archive files", "jar"));
		
		if( fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
			File f = fc.getSelectedFile();
			
			AndroidDrawersRegistry.getDrawerFromJar(f.getAbsolutePath());
		}
		/*String path = "C:\\rapid.jar";
		getDrawerFromJar(path);*/
	}
}
